﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using WebPrice.Models;

namespace WebPrice.DAL
{
    public class Pricing_Initializer :System.Data.Entity.DropCreateDatabaseIfModelChanges<PricingDbContext>
    {
        private PricingDbContext _context;
        protected override void Seed(PricingDbContext context)
        {
            _context = context;
            GetData();
        }

        private int pricesereiesId;
        public List<Instrument> GetData()
        {
            pricesereiesId = 0;
            var instrumentList = new List<Instrument>();
            var random = new Random();
            for (int i = 0; i < 100; i++)
            {
                var instrument = new Instrument();
                var name = randomName(random);
                instrument.Name = name.ToString();
                instrument.Id = i;
                instrument.InstrumentTypeId = random.Next(3);
                instrument.Active = random.Next(5) != 1;
                GetShareClassList(instrument.ShareClasses, random, i);
                instrumentList.Add(instrument);
                
            }
            instrumentList.ForEach(s => _context.Instruments.Add(s));
            _context.SaveChanges();
            //using (var ctx = new CombinedDbContext())
            //{

            //    instrumentList.ForEach(x => ctx.Instruments.Add(x));
            //    ctx.SaveChanges();
            //}


            return instrumentList;
        }

        private void GetShareClassList(ICollection<ShareClass> ShareClassList, Random random, int instrumentId)
        {

            for (int i = 0; i <= instrumentId % 4; i++)
            {
                var ShareClass = new ShareClass();
                ShareClass.Name = "Class " + randomName(random);
                ShareClass.Id = pricesereiesId++;
                ShareClass.InstrumentId = instrumentId;
                ShareClass.IsMaster = i == 0;
                ShareClass.TrackRecordType = random.Next(2);
                GetShareClassPrices(random, ShareClass);
                GetShareClassDividends(random, ShareClass);
                GetShareClassStatistics(random, ShareClass);
                _context.ShareClasses.Add(ShareClass);
               // ShareClassList.Add(ShareClass);
            }
        }

        private void GetShareClassPrices(Random random, ShareClass ShareClass)
        {
            DateTime date = new DateTime(2013, 1, 1);
            var firstPrice = new Price { Currency = "USD", Nav = 1000, PriceQuality = 1, ShareClassId = ShareClass.Id, Ror = 0, ValueAsOf = date };

            var nav = firstPrice.Nav;
            for (int i = 0; i <= ShareClass.Id % 10; i++)
            {

                date = date.AddMonths(1);

                var price = new Price();
                price.ValueAsOf = date;
                price.Ror = random.NextDouble();
                nav = (1 + price.Ror) * nav;
                price.Nav = nav;
                price.PriceQuality = i;
                price.ShareClassId = ShareClass.Id;
                _context.Prices.Add(price);
                //ShareClass.PriceList.Add(price);
            }
        }

        private void GetShareClassDividends(Random random, ShareClass ShareClass)
        {
            DateTime date = new DateTime(2013, 1, 1);

            //for (int i = 0; i <= ShareClass.Id % 5; i++)
            //{

            //    date = date.AddMonths(1);

            //    var dividend = new Dividend();
            //    dividend.ValueAsOf = date;
            //    dividend.Ror = random.NextDouble();
            //    dividend.ShareClassId = ShareClass.Id;
            //    ShareClass.DividendList.Add(dividend);
            //}
        }

        private void GetShareClassStatistics(Random random, ShareClass ShareClass)
        {


            ////  if (ShareClass.Id % 5 > 4)
            //{
            //    ShareClass.Statistic = new Statictics();
            //    ShareClass.Statistic.Average = random.NextDouble();
            //    ShareClass.Statistic.Count = random.Next(1000);
            //    ShareClass.Statistic.Proportions = random.NextDouble();
            //    ShareClass.Statistic.Sum = random.NextDouble() * random.Next(10000);
            //}
        }


        private static StringBuilder randomName(Random random)
        {
            var name = new StringBuilder(16);
            for (int j = 0; j < 15; j++)
            {
                int val = random.Next() % 26;
                char letter = (char)('a' + val);
                name.Append(letter);
            }
            return name;
        }
        
    }
}